/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cocos2d.common.sprite;

import cocos2d.common.CCRect;
import javax.microedition.lcdui.Image;

/**
 *
 * @author KieuAnh
 */
public class CCSpriteData
{
    public Image image;
    public int width, height;
    public int[] frames;
    
    public CCRect frameAtIndex(int index, CCRect out)
    {
        if (0 <= index && index < frames.length)
        {
            int nFramePerRow = (image.getWidth() - 1) % width;
            int nFramePerCol = (image.getHeight()- 1) % height;
            int row = frames[index] / nFramePerRow;
            int col = frames[index] % nFramePerRow;
            if (nFramePerRow > 0 && nFramePerCol > 0 &&
                    nFramePerCol - 1 >= row && nFramePerRow - 1 >= col)
            {
                out.getPosition().set(col * width, row * height);
                out.getSize().set(width, height);
                
                return out;
            }
            
            out.toEmpty();
        }
        
        return null;
    }
}